package net.cctech.product_service.service.impl;

import net.cctech.product_service.dao.ProductRepository;
import net.cctech.product_service.domain.Product;
import net.cctech.product_service.service.ProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductRepository productRepository;

    private static final Map<Integer, Product> daoMap = new HashMap<>();
    private final Logger logger = LoggerFactory.getLogger(getClass());

    static {

        Product p1 = new Product(1,"iphonex",new BigDecimal(999), 10);
        Product p2 = new Product(2,"冰箱",new BigDecimal(5342), 19);
        Product p3 = new Product(3,"洗衣机",new BigDecimal(523), 90);
        Product p4 = new Product(4,"电话",new BigDecimal(64345), 150);
        Product p5 = new Product(5,"汽车",new BigDecimal(2345), 140);
        Product p6 = new Product(6,"椅子",new BigDecimal(253), 20);
        Product p7 = new Product(7,"java编程思想",new BigDecimal(2341), 10);

        daoMap.put(p1.getId(),p1);
        daoMap.put(p2.getId(),p2);
        daoMap.put(p3.getId(),p3);
        daoMap.put(p4.getId(),p4);
        daoMap.put(p5.getId(),p5);
        daoMap.put(p6.getId(),p6);
        daoMap.put(p7.getId(),p7);
    }




    @Override
    public List<Product> listProduct() {

        Collection<Product> collection = daoMap.values();
        List<Product> list = new ArrayList<>(collection);

        return list;
    }

    @Override
    public Product findById(int id) {
        logger.info("service findById");
        return daoMap.get(id);
    }

    @Override
    public Product findDbById(int id) {
        return productRepository.findOneById(id);
    }

    @Override
    public Boolean save() {
        Product product = new Product();
        product.setName(UUID.randomUUID().toString());
        product.setStore(1);
        product.setPrice(new BigDecimal(99));
        productRepository.save(product);
        return Boolean.TRUE;
    }
}
